package com.md.youyouhui.ui.home;

import android.databinding.DataBindingUtil;
import android.os.Build;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityBannerShareBinding;
import com.md.youyouhui.net.Constants;
import com.md.youyouhui.net.NetWorkConstant;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.SharedPreferencesUtils;
import com.md.youyouhui.utils.StatusBarHelper;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * Created by AdministratorZJ
 * on 2019/4/30.
 */
public class BannerShareActivity extends BaseActivity {
    private ActivityBannerShareBinding shareBinding;

    @Override
    public void setContentLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StatusBarHelper.setFullWindow(getWindow());
            StatusBarHelper.setStatusBarTextColor(getWindow(), false);
        }
        shareBinding = DataBindingUtil.setContentView(this, R.layout.activity_banner_share);
    }

    @Override
    public void initView() {
        shareBinding.back.setOnClickListener(v -> finish());
    }

    @Override
    public void initData() {
        shareBinding.share.setOnClickListener(v -> {
            shareQQ();
            //Toast.makeText(ct, "敬请期待", Toast.LENGTH_SHORT).show();
        });
    }


    private void shareQQ() {
        //Toast.makeText(ct, "分享正在后台进行", Toast.LENGTH_SHORT).show();
        String code = SharedPreferencesUtils.getString(ct, Constants.INVITED_CODE, "");
        //分享
        MobclickAgent.onEvent(this, "Forward");
        //umeng share
        UMWeb web = new UMWeb(NetWorkConstant.BASE_URL + "invited/invited/receive?source=" + Constants.SOURCE + "&invitedCode=" + code);
        //标题
        web.setTitle("欢迎使用油有惠");
        //缩略图
        web.setThumb(new UMImage(this, R.mipmap.app_icon));
        //描述
        web.setDescription("欢迎使用油有惠");
        new ShareAction(this).withMedia(web).setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                .setCallback(shareListener).open();
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
            Toast.makeText(ct, "分享正在后台进行", Toast.LENGTH_SHORT).show();
        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(BannerShareActivity.this, "分享成功", Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(BannerShareActivity.this, "分享失败" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(BannerShareActivity.this, "取消分享", Toast.LENGTH_LONG).show();

        }
    };
}
